﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.OA.Utils;
using Ccit.Utils;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Ccit.OA.NewsCenter
{
    public partial class news_page_set : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack){
                _hidType.Value = (null != Request.QueryString["Type"]) ? Request.QueryString["Type"].ToString() : "0";
            }
            initializePage();
        }
        private void initializePage()
        {
            LoginUser login = (LoginUser)Session["LogUserInfo"];
            string userId = login.UserId;

            LayoutBLL layoutbll = new LayoutBLL();

          //  bool checkOper = layoutbll.CheckSysPlate(_hidType.Value, userId);

            //获取当前用户的布局设置
            MsLayout layoutObj = layoutbll.GetLayOutObj(userId,_hidType.Value);
           
            WindowBLL winbll = new WindowBLL();

            if (layoutObj == null)  //没有布局
            {
                layoutObj = layoutbll.GetLayOutDefault(_hidType.Value);  //取系统板块的布局
               // _hidSysUserId.Value = layoutObj.UserId;
                //rdTwo.Checked = true;
                //txtOneCell.Text = "50";
                //txtTwoCell.Text = "50";
                //List<MsWinStyle> winList = winbll.GetAllPlate(_hidType.Value);
                //foreach(MsWinStyle winStyle in winList){            //待选板块
                //    lstUnSelPlate.Items.Add(new ListItem(winStyle.PlateName,winStyle.PlateId));
                //} 
            }    
            if(layoutObj != null)   //有布局
            {
                _hidSysUserId.Value = layoutObj.UserId;
                switch (layoutObj.CellCount)
                {
                    case "1":
                        rdOne.Checked = true;
                        break;
                    case "2":
                        rdTwo.Checked = true;
                        break;
                    case "3":
                        rdThree.Checked = true;
                        break;
                }
                txtOneCell.Text = layoutObj.OneCellWidth;
                txtTwoCell.Text = layoutObj.TwoCellWidth;
                txtThreeCell.Text = layoutObj.ThreeCellWidth;


                List<MsWinStyle> winList = null;

                if (layoutObj.UserId == "-999")
                {
                    winList = winbll.GetSysUnSelPlate(_hidType.Value);
                }
                else
                {
                    winList = winbll.GetUnSelPlate(_hidType.Value);  // 待选板块
                }

                foreach (MsWinStyle winStyle in winList)
                {
                    lstUnSelPlate.Items.Add(new ListItem(winStyle.PlateName, winStyle.PlateId));
                }

                List<MsWinStyle> winOneList = null;  //一列已选板块
                if (layoutObj.UserId == "-999")
                {
                    winOneList = winbll.GetSysWinShowCell("1", _hidType.Value);
                }
                else
                {
                    winOneList = winbll.GetWinShowCell("1", _hidType.Value);
                }

                foreach (MsWinStyle winOneStyle in winOneList)
                {
                    _hidSelPlateId.Value += winOneStyle.PlateId+",";
                    lstOneCellSel.Items.Add(new ListItem(winOneStyle.PlateName, winOneStyle.PlateId));
                }

                List<MsWinStyle> winTwoList = null;  //二列已选板块
                
                if (layoutObj.UserId == "-999")
                {
                    winTwoList = winbll.GetSysWinShowCell("2", _hidType.Value);
                }
                else
                {
                    winTwoList = winbll.GetWinShowCell("2", _hidType.Value);
                }
                foreach (MsWinStyle winTwoStyle in winTwoList)
                {
                    _hidSelPlateId.Value += winTwoStyle.PlateId + ",";
                    lstTwoCellSel.Items.Add(new ListItem(winTwoStyle.PlateName, winTwoStyle.PlateId));
                }

                List<MsWinStyle> winThreeList = null;    //三列已选版块
                if (layoutObj.UserId == "-999")
                {
                    winThreeList = winbll.GetSysWinShowCell("3", _hidType.Value);
                }
                else
                {
                    winThreeList = winbll.GetWinShowCell("3", _hidType.Value);
                }
                
                foreach (MsWinStyle winThreeStyle in winThreeList)
                {
                    _hidSelPlateId.Value += winThreeStyle.PlateId + ",";
                    lstThreeCellSel.Items.Add(new ListItem(winThreeStyle.PlateName, winThreeStyle.PlateId));
                }
            }

        }

        #region  Ajax 方法
        [global::System.Web.Services.WebMethod]
        public static string SavePlate(string jsonExpr, string selOnePlate, string selTwoPlate, string selThreePlate,string unselPlate,string Type,string SysUserId)
        {
            MsLayout layout=(MsLayout)JavaScriptConvert.DeserializeObject(jsonExpr,typeof(MsLayout));
            LayoutBLL bll = new LayoutBLL();
            List<string> strlist = bll.SaveMsLayOut(layout, selOnePlate, selTwoPlate, selThreePlate, unselPlate, Type, SysUserId);
            var oResult =new { resFlag=strlist[0],resMes=strlist[1] };
            return JavaScriptConvert.SerializeObject(oResult); ;
        }
        #endregion

    }
}
